package com.ykqx.weather.service;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ykqx.weather.app.bo.AppUpdateInf;
import com.ykqx.weather.app.bo.CONSTANT;
import com.ykqx.weather.app.bo.ResponseMessage;
import com.ykqx.weather.app.util.ServletResponseUtil;
import com.ykqx.weather.config.datasource.DatasourceKey;
import com.ykqx.weather.config.datasource.TargetDataSource;
import com.ykqx.weather.dao.AppUpdateInfDao;
import com.ykqx.weather.dto.AppUpdate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/9/4 0004
 */
@Service
@TargetDataSource(DatasourceKey.DB2)
public class AppUpdateService {
    Gson gson = new GsonBuilder().create();
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    AppUpdateInfDao appUpdateInfDao;

    public AppUpdate updateApp(){

        AppUpdateInf appInf = appUpdateInfDao.getNewestAppUpdateInf();
        AppUpdate appUpdate = new AppUpdate(appInf.getAppVersion(),CONSTANT.APP_DOWNLOAD_PATH);

        return appUpdate;
    }


    public String addAppInf(String version) {
        try {
            int result = appUpdateInfDao.addAppInf(version);
            logger.info("新增app版本信息成功->{}", result);
            return gson.toJson(new ResponseMessage(CONSTANT.RESPONSE_SUCCESS, "新增信息成功"));
        } catch (Exception e) {
            logger.error("新增app版本信息失败", e.getMessage());
            return gson.toJson(new ResponseMessage(CONSTANT.RESPONSE_FAILED, "新增信息失败"));
        }
    }

    public String setAppUpdateAdmin(String status, String version) {
        try {
            int result = appUpdateInfDao.setAppUpdateAdmin(status, version);
            logger.info("修改app更新权限成功->{}", result);
            return gson.toJson(new ResponseMessage(CONSTANT.RESPONSE_SUCCESS, "更新权限成功"));
        } catch (Exception e) {
            logger.error("修改app更新权限失败", e.getMessage());
            return gson.toJson(new ResponseMessage(CONSTANT.RESPONSE_FAILED, "更新权限失败"));
        }
    }
}
